//
//  SSSPersonalIncomePagerViewController.m
//  SSSMerchant
//
//  Created by xnxin on 2017/3/22.
//  Copyright (c) 2017 xnxin. All rights reserved.
//

#import "SSSPersonalIncomePagerViewController.h"
#import <Masonry/Masonry.h>
#import "UIColor+Hexadecimal.h"
#import "UIViewAdditions.h"
#import "SSSPersonalPointsViewController.h"
#import "SSSPersonalGoodPointViewController.h"
#import "scanDetailController.h"

@interface SSSPersonalIncomePagerViewController ()

@property (nonatomic, strong) UIView *topView;
///今日善心
@property (nonatomic, strong) UILabel *todayGoodPoint;
@property (nonatomic, strong) UIButton *seeDatailButton;

@end

@implementation SSSPersonalIncomePagerViewController

- (NSArray<NSString *> *)titles {
    return @[@"我的积分", @"我的善点"];
}

- (void)loadView {
    [super loadView];

    [self initialiseMenu];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.progressViewCornerRadius = 10;
    self.progressViewCorners = UIRectCornerTopLeft|UIRectCornerTopRight;

    self.title = @"个人收益";
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = QMUICMI.backgroundColor;
    self.topView.backgroundColor = self.view.backgroundColor;
    self.viewFrame = CGRectMake(0, 172 + 15, DEVICE_WIDTH, self.view.height - 172 - 64 - 15);
    [self setupProgressPath];

}

- (void)setupProgressPath {

    CGRect rect = CGRectMake(15, 0, DEVICE_WIDTH - 30, self.menuView.progressView.height);
    UIView *v = [[UIView alloc] initWithFrame:rect];

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:v.bounds
                                               byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight
                                                     cornerRadii:CGSizeMake(self.progressViewCornerRadius, self.progressViewCornerRadius)];

    CAShapeLayer * layer = [[CAShapeLayer alloc]init];
    layer.path = path.CGPath;
    layer.frame = v.bounds;
    v.layer.mask = layer;
    v.backgroundColor = [UIColor colorWithHex:@"E6E6E6"];

    [self.menuView insertSubview:v atIndex:0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initialiseMenu {


    CGFloat  padding = 15;
    self.menuHeight = 45;
    self.menuItemWidth = (DEVICE_WIDTH - 2 * padding)/2;
    self.menuViewStyle = WMMenuViewStyleFlood;
    self.menuViewLayoutMode = WMMenuViewLayoutModeCenter;
    self.titleColorNormal = [UIColor whiteColor];
    self.titleSizeNormal = 14;
    self.menuBGColor = [UIColor clearColor];
    self.titleSizeSelected = 14;
    self.titleColorSelected = [UIColor whiteColor];
    self.progressColor = [UIColor colorWithHex:@"55A987"];
    self.progressHeight = 45;
    self.progressViewBottomSpace = 5;
    self.itemMargin = 1;
    self.menuViewBottomSpace = 1;
}


#pragma mark - getter

- (UIView *)topView {

    if (!_topView) {

        _topView = [UIView new];
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top);
            make.leading.equalTo(self.view.mas_leading);
            make.trailing.equalTo(self.view.mas_trailing);
            make.height.equalTo(@(172));
        }];

        UIImageView *_bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_bg"]];
        [_topView addSubview:_bgView];


        UILabel *todayLabel = [UILabel new];
        todayLabel.text = @"今日善心";
        todayLabel.textColor = [UIColor colorWithHex:@"F5F5F5"];
        todayLabel.font = [UIFont systemFontOfSize:15];
        [_topView addSubview:todayLabel];
        [todayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).offset(25);
            make.centerX.equalTo(self.view.mas_centerX);
        }];

        [_topView addSubview:self.todayGoodPoint];
        [self.todayGoodPoint mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(todayLabel.mas_centerX);
            make.top.equalTo(todayLabel.mas_top).offset(24);
        }];

        [_topView addSubview:self.seeDatailButton];
        [self.seeDatailButton mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.todayGoodPoint.mas_bottom).offset(10);
            make.leading.equalTo(self.todayGoodPoint.mas_trailing);
        }];
    }
    return _topView;
}

- (UILabel *)todayGoodPoint {

    if(!_todayGoodPoint) {

        _todayGoodPoint = [UILabel new];
        _todayGoodPoint.font = [UIFont systemFontOfSize:55];
        _todayGoodPoint.text = @"0.00";
        _todayGoodPoint.textColor = [UIColor whiteColor];
    }
    return _todayGoodPoint;
}

- (UIButton *)seeDatailButton {

    if (!_seeDatailButton) {

        _seeDatailButton = [UIButton new];
        [_seeDatailButton setImage:[UIImage imageNamed:@"icon_eyes"] forState:UIControlStateNormal];
        [_seeDatailButton setTitle:@" 查看详情" forState:UIControlStateNormal];
        _seeDatailButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [_seeDatailButton setTitleColor:[UIColor colorWithHex:@"FEFEFE"] forState:UIControlStateNormal];
        [_seeDatailButton addTarget:self action:@selector(seeGoodsPointTrend) forControlEvents:UIControlEventTouchUpInside];
    }
    return _seeDatailButton;
}


- (void)seeGoodsPointTrend {

    UIViewController *vc = [scanDetailController new];
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark - WMPageCtrl
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titles.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.titles[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController
               viewControllerAtIndex:(NSInteger)index
{

    if (index == 0) {
        return [SSSPersonalPointsViewController new];
    }

    if (index == 1) {
        return [SSSPersonalGoodPointViewController new];
    }

    return nil;
}

@end
